﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.Data.Common;
using System.ComponentModel;

namespace Tilver.DBConnectionUI
{
    /// <summary>
    /// simple class wrapper to allow me for hiding 
    /// </summary>
    internal class ObjectWrapper : ICustomTypeDescriptor
    {

        object selectedObject;
        public ObjectWrapper(object _selectedObject)
        {
            HiddenProperties = new List<string>();
            selectedObject = _selectedObject;
        }

        public List<string> HiddenProperties { get; private set; }

        #region ICustomTypeDescriptor Members
        // Implementation of ICustomTypeDescriptor: 

        public String GetClassName()
        {
            return TypeDescriptor.GetClassName(selectedObject, true);
        }

        public AttributeCollection GetAttributes()
        {
            return TypeDescriptor.GetAttributes(selectedObject, true);
        }

        public String GetComponentName()
        {
            return TypeDescriptor.GetComponentName(selectedObject, true);
        }

        public TypeConverter GetConverter()
        {
            return TypeDescriptor.GetConverter(selectedObject, true);
        }

        public EventDescriptor GetDefaultEvent()
        {
            return TypeDescriptor.GetDefaultEvent(selectedObject, true);
        }

        public PropertyDescriptor GetDefaultProperty()
        {
            return TypeDescriptor.GetDefaultProperty(selectedObject, true);
        }

        public object GetEditor(Type editorBaseType)
        {
            return TypeDescriptor.GetEditor(selectedObject, editorBaseType, true);
        }

        public EventDescriptorCollection GetEvents(Attribute[] attributes)
        {
            return TypeDescriptor.GetEvents(selectedObject, attributes, true);
        }

        public EventDescriptorCollection GetEvents()
        {
            return TypeDescriptor.GetEvents(selectedObject, true);
        }

        public object GetPropertyOwner(PropertyDescriptor pd)
        {
            return selectedObject;
        }

        public PropertyDescriptorCollection GetProperties(Attribute[] attributes)
        {
            return GetProperties();
        }

        public virtual PropertyDescriptorCollection GetProperties()
        {
            PropertyDescriptorCollection source =
                TypeDescriptor.GetProperties(selectedObject, false);

            if (HiddenProperties.Count <= 0)
                return source;
            else
            {
                PropertyDescriptorCollection propsCopy = new PropertyDescriptorCollection(null);
                foreach (PropertyDescriptor prop in source)
                {
                    if(HiddenProperties.Contains(prop.Name))
                        continue;
                    propsCopy.Add(prop);
                }
                return propsCopy;
            }
        }
        #endregion

    }
}
